//
//  OnAuction.swift
//  LeGou
//
//  Created by lxm on 2022/11/28.
//

import UIKit

class OnAuction: ZMViewController, UITableViewDelegate, UITableViewDataSource {

    //MARK:--------------------属性--------------------
    @IBOutlet weak var playBtn: UIButton!
    
    var player : TestPlayer!
    
    @IBOutlet weak var stateLabel: UILabel!
    
    var logs = [String]()
    
    //MARK:--------------------UI控件--------------------
    @IBOutlet weak var tableView: UITableView!
    
    let contentView = UIView()

    //MARK:--------------------生命周期--------------------
    override func viewDidLoad() {
        super.viewDidLoad()
        setUI()
        addConstraints()
        bindAction()
        player.onLog = { [weak self] str in
            guard let self = self else {return}
            self.logs.insert(str, at: 0)
            self.tableView.reloadData()
        }
    }
    //MARK:--------------------UI--------------------
    func setUI() {
        title = ""
        tableView.delegate = self
        tableView.dataSource = self
        tableView.estimatedRowHeight = 44
        tableView.rowHeight = UITableView.automaticDimension
        stateLabel.numberOfLines = 0
        stateLabel.text = "当前用户:\(player.model.phone)_id:\(player.model.uid)\ntoken:\(player.model.token)_showId:\(player.model.showId)"
    }
    
    func addConstraints() {
        
    }
    
    //MARK:--------------------methods--------------------
    func bindAction() {
        
    }

    @IBAction func onPlay(_ sender: Any) {
        if player.playStatus {
            player.playStatus = false
            playBtn.setTitle("play", for: .normal)
        } else {
            player.playStatus = true
            player.playOnce()
            playBtn.setTitle("运行中", for: .normal)
        }
    }
    
    //MARK:--------------------delegate--------------------
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        logs.count
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        var cell = tableView.dequeueReusableCell(withIdentifier: "cell")
        if cell == nil {
            cell = UITableViewCell.init(style: .default, reuseIdentifier: "cell")
        }
        let data = logs[indexPath.row]
        
        if data.contains("详情") {
            cell?.textLabel?.textColor = UIColor.red
        } else {
            cell?.textLabel?.textColor = UIColor.zm.color333333()
        }
        
        cell?.textLabel?.text = data
        
        cell?.textLabel?.numberOfLines = 0
        
        return cell!
    }
}
